﻿
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace FrameEngine
{
    /// <summary>
    /// 基础的主操作类
    /// </summary>
    public class BaseApplication : IApplication
    {
        /// <summary>
        /// 系统中所有命令的列表
        /// </summary>
        private readonly Dictionary<string, List<ICommand>> _commandD = new Dictionary<string, List<ICommand>>();
     
        /// <summary>
        /// 主UI
        /// </summary>
        private readonly FrameworkElement _globeControl;

        /// <summary>
        /// 当前的工具
        /// </summary>
        private ITool _currentTool = new NullTool();


        /// <summary>
        /// 当前是否可以切换工具
        /// </summary>
        private bool _isCanChangeTool = true;


        /// <summary>
        /// 基础的主操作类
        /// </summary>
        /// <param name="pGlobeControl"></param>
        public BaseApplication(FrameworkElement pGlobeControl)
        {
            _globeControl = pGlobeControl;
            _globeControl.MouseDown += GlobeControl_MouseDown;
            _globeControl.MouseMove += GlobeControl_MouseMove;
            _globeControl.MouseUp += GlobeControl_MouseUp;
           // _globeControl.MouseDoubleClick += GlobeControl_MouseDoubleClick;

            this.PanTool = new NullTool();
        }

        /// <summary>
        /// 主窗体
        /// </summary>
        public Window MainWindow { get; set; }
        /// <summary>
        /// 主程序标题
        /// </summary>
        public string Caption { get; set; }
        #region IApplication Members

        /// <summary>
        /// 主操作控件 所有工具Itool所操作的对象
        /// </summary>
        public FrameworkElement GlobeControl
        {
            get { return _globeControl; }
        }


        /// <summary>
        /// 是否可以切换工具
        /// </summary>
        public bool IsCanChangeTool
        {
            get
            {
                if (_isCanChangeTool)// && !GlobeControl.IsUsingPersistentTool())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set { _isCanChangeTool = value; }
        }


        /// <summary>
        /// 状态栏
        /// </summary>
        public IStateBar StateBar { get; set; }

        /// <summary>
        /// 用户信息栏
        /// </summary>
        public IStateBar UserInfoBar { get; set; }
        /// <summary>
        /// 设置或得到当前工具
        /// </summary>
        public ITool CurrentTool
        {
            get { return _currentTool; }
            set
            {
                if (!IsCanChangeTool)
                {
                    return;
                }
                ITool myValue = value;
                if (myValue == null)
                {
                    myValue = new NullTool();
                }
                ITool myLastTool = _currentTool;
                //如果当前选择的还是当前工具
                if (myLastTool == myValue)
                {
                    return;
                }

                myLastTool.UnSelect();
                myValue.Select();
                _currentTool = value;
            }
        }


        /// <summary>
        /// 系统的平移工具
        /// </summary>
        public ITool PanTool { get; set; }


        /// <summary>
        /// 根据关键字，得到命令列表
        /// </summary>
        /// <param name="pKey"></param>
        /// <returns></returns>
        public List<ICommand> GetCommandsByKey(string pKey)
        {
            List<ICommand> myCommandList = null;
            _commandD.TryGetValue(pKey, out myCommandList);
            if (myCommandList == null)
            {
                myCommandList = new List<ICommand>();
            }
            return myCommandList;
        }


        /// <summary>
        /// 注册命令
        /// </summary>
        /// <param name="pCommand"></param>
        public void RegisterCommand(ICommand pCommand)
        {
            List<ICommand> myCommandList = null;
            _commandD.TryGetValue(pCommand.CommandName, out myCommandList);
            if (myCommandList == null)
            {
                myCommandList = new List<ICommand>();
                _commandD.Add(pCommand.CommandName, myCommandList);
            }
            myCommandList.Add(pCommand);
        }

        #endregion

        private void GlobeControl_MouseDown(object sender, MouseEventArgs e)
        {
            _currentTool.MouseDown(e); 
        }


        private void GlobeControl_MouseMove(object sender, MouseEventArgs e)
        {
            _currentTool.MouseMove(e);
        }


        private void GlobeControl_MouseUp(object sender, MouseEventArgs e)
        {
            _currentTool.MoudeUp(e);
        }


        private void GlobeControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            _currentTool.MouseDoubleClick(e);
        }
    }
}
